﻿package mortal.game.view.common.tileList
{
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.primitives.object2d.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.view.common.*;

    public class G3DCellRenderer extends GCellRenderer
    {
        protected var bg:Bitmap;
        protected var itemBg:Img2D;
        protected var bodyPlayer:ActionPlayer;
        protected var hasAddToStage:Boolean;
        protected var hasRender:Boolean;
        protected var isResCompl:Boolean;

        public function G3DCellRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildren();
            this.configEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            this.configEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveHandler);
            return;
        }// end function

        protected function onAddedHandler(event:Event) : void
        {
            if (event.target is G3DCellRenderer)
            {
                this.add3DModel();
            }
            return;
        }// end function

        protected function onRemoveHandler(event:Event) : void
        {
            if (event.target is G3DCellRenderer && this.rect3d)
            {
                this.rect3d.disposeObj3d(this.bodyPlayer);
                this.bodyPlayer = null;
                this.rect3d.removeImg(this.itemBg);
            }
            return;
        }// end function

        override protected function onAddedToStageHandler(event:Event) : void
        {
            super.onAddedToStageHandler(event);
            this.hasAddToStage = true;
            this.add3DModel();
            return;
        }// end function

        protected function add3DModel() : void
        {
            if (this.rect3d)
            {
                this.rect3d.disposeObj3d(this.bodyPlayer);
                this.bodyPlayer = null;
                this.rect3d.removeImg(this.itemBg);
            }
            return;
        }// end function

        public function get g3DTileList() : G3DTileList
        {
            if (this.parent && this.parent.parent)
            {
                return this.parent.parent.parent as G3DTileList;
            }
            return null;
        }// end function

        public function get rect3d() : Rect3DObject
        {
            if (this.g3DTileList)
            {
                return this.g3DTileList.rect3DObject;
            }
            return null;
        }// end function

        public function get bgPos() : Point
        {
            var _loc_1:Point = null;
            var _loc_2:Point = null;
            if (this.g3DTileList)
            {
                _loc_1 = localToGlobal(new Point(this.g3DTileList.offsetX, this.g3DTileList.offsetY));
                _loc_2 = this.g3DTileList.globalToLocal(_loc_1);
            }
            else
            {
                _loc_2 = new Point();
            }
            return _loc_2;
        }// end function

        public function get canAdd3DModel() : Boolean
        {
            return _data && this.hasAddToStage && this.isResCompl;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            _data = null;
            this.hasRender = false;
            this.isResCompl = false;
            this.hasAddToStage = false;
            if (this.bg)
            {
                UIFactory.disposeBitmap(this.bg);
                this.bg = null;
            }
            if (this.itemBg)
            {
                this.itemBg = null;
            }
            if (this.bodyPlayer)
            {
                this.bodyPlayer.dispose(param1);
                this.bodyPlayer = null;
            }
            return;
        }// end function

    }
}
